package com.dpstudio.ymprestlful.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dpstudio.ymprestlful.beans.Request;
import com.dpstudio.ymprestlful.utils.convert.ParamConvert;
import com.dpstudio.ymprestlful.view.window.frame.RestDetail;
import com.intellij.openapi.project.Project;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class HttpClientFileUtils {

    public static void writeHead(Project project, Request request, FileWriter fileWriter, File fileDir, String reqUrl) throws IOException {
        Map<String, Object> headerMap = new HashMap<>();
        if (RestUtil.scanCacheType(project, request.getPsiMethod().getResolveScope())) {
            File jsonFile = new File(fileDir.getPath(), "restful.json");
            if (jsonFile.exists()) {
                JSONObject json = JSONUtil.readJSONObject(jsonFile, Charset.defaultCharset());
                String headKey = reqUrl + "_" + request.getMethod().name().toUpperCase(Locale.ROOT) + "_head";
                if (StringUtils.isNotBlank(json.getStr(headKey)) && json.getJSONObject(headKey) != null) {
                    headerMap.putAll(json.getJSONObject(headKey));
                }
            }
        } else {
            if (StringUtils.isNotBlank(RestDetail.headersCache.get(request))) {
                headerMap.putAll(JSONUtil.parseObj(RestDetail.headersCache.get(request)));
            }
        }
        if (MapUtil.isEmpty(headerMap)) {
            headerMap.putAll(RestUtil.scanListenerRequestHeaders(project, request.getPsiMethod().getResolveScope()));
        }
        if (MapUtil.isNotEmpty(headerMap)) {
            headerMap.forEach((k, v) -> {
                try {
                    fileWriter.write(k + ": " + v + "\n");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            });
            fileWriter.write("\n");
        }


    }


    public static void writeBody(Project project, Request request, FileWriter fileWriter, File fileDir, String reqUrl, ParamConvert paramConvert) throws IOException {
        String bodyStr = "";
        if (RestUtil.scanCacheType(project, request.getPsiMethod().getResolveScope())) {
            File jsonFile = new File(fileDir.getPath(), "restful.json");
            if (jsonFile.exists()) {
                JSONObject json = JSONUtil.readJSONObject(jsonFile, Charset.defaultCharset());
                String bodyKey = reqUrl + "_" + request.getMethod().name().toUpperCase(Locale.ROOT) + "_body";
                if (StringUtils.isNotBlank(json.getStr(bodyKey)) && json.getJSONObject(bodyKey) != null) {
                    bodyStr = json.getStr(bodyKey);
                }
            }
        } else {
            if (StringUtils.isNotBlank(RestDetail.bodyCache.get(request))) {
                bodyStr = RestDetail.bodyCache.get(request);
            }
        }
        if (StringUtils.isBlank(bodyStr)) {
            bodyStr = paramConvert.formatString();
        }
        fileWriter.write(bodyStr + "\n");
        fileWriter.write("\n\n");
    }
}
